<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/4/13
 * Time: 上午11:11
 *
 * 功能：系统自动取消退款
 * 版本：v1.0
 *
 *
 * 规则：1分钟拉取一次待处理数据
 *
 * 注意：可以手动，如果手动处理，则可以修复指定一个分钟内的未正确取消的退款单
 *      例如 php cancelRefund.php 2018-03-20 11:19:00  修复指定时间的未取消的退款单
 *
 *
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\service\trade\OrderRefund as sOrderRefund;


if (isset($argv[1])) {
    $stopMiunte = $argv[1];
} else {
    $stopMiunte = date('Y-m-d H:i:00', time());
}

$startMiunte = date('Y-m-d H:i:00', strtotime($stopMiunte) - 60);


$where = [
    'auto_cancel_time[<>]' => [$startMiunte, $stopMiunte],
    'status'              => [mOrderRefund::STATUS_NORMAL,mOrderRefund::STATUS_REJECT,mOrderRefund::STATUS_WAIT_REFUND_GOODS],
];

try {
    //取出待处理的数据
    $orderRefundList = mOrderRefund::select("*", $where);

    if (empty($orderRefundList)) {
        throw new \Exception("当前时刻没有待处理的退款单数据");
    }

    foreach ($orderRefundList as $orderRefund) {

        try {

            sOrderRefund::cancelRefund($orderRefund['uid'], $orderRefund['refund_order_no'], true);

        } catch (\Exception $e) {

            echo sprintf("%s    时刻:%s   refund_order_no:%s   异常：%s  \n", date('Y-m-d H:i:s'),$stopMiunte, $orderRefund['refund_order_no'],json_encode([
                'e_file'=>$e->getFile(),
                'e_line'=>$e->getLine(),
                'e_msg'=>$e->getMessage()
            ],JSON_UNESCAPED_UNICODE));

        }finally{
            echo sprintf("%s    时刻:%s    refund_order_no:%s   sql日志：%s   \n",date('Y-m-d H:i:s'),$stopMiunte,$orderRefund['refund_order_no'],mOrderRefund::sqlLogs());

            mOrderRefund::clearLogs();
        }
    }
} catch (\Exception $e) {

    echo sprintf("%s    %s 时刻:%s\n", date('Y-m-d H:i:s'), $e->getMessage(), $stopMiunte);

}






